package cn.wolfcode.trip.admin.web.controller;


import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.query.StrategyCatalogQueryObject;
import cn.wolfcode.trip.base.service.IStrategyCatalogService;
import cn.wolfcode.trip.base.service.IStrategyService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@RequestMapping("/strategyCatalog")
@Controller
public class StrategyCatalogController {
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyService strategyService;


    @RequestMapping("/getStrategyCatalogByStrategyId")
    @ResponseBody
    public Object getStrategyCatalogByStrategyId(Long strategyId){
        List<StrategyCatalog> list=strategyCatalogService.getStrategyCatalogByStrategyId(strategyId);
        return list;
    }


    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") StrategyCatalogQueryObject qo) {
        qo.setPageSize(10);
        PageInfo pageInfo = strategyCatalogService.getPageInfo(qo);
        model.addAttribute("result", pageInfo);
        model.addAttribute("strategies", strategyService.list());
        return "/strategyCatalog/list";
    }

    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(StrategyCatalog strategyCatalog) {
        JSONResult result = new JSONResult();

        try {
            strategyCatalogService.saveOrUpdate(strategyCatalog);
        }catch(Exception e){
            e.printStackTrace();
            result.mark(e.getMessage());
        }

        return result;

    }

}
